package com.ecoolex.ying.space.core.manager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.common.util.CollectorUtil;
import com.ecoolex.ying.space.core.dao.EstateProjectActivityHouseModelItemsDAO;
import com.ecoolex.ying.space.entity.EstateProjectActivityGoods;
import com.ecoolex.ying.space.entity.EstateProjectActivityHouseModelItems;
import com.ecoolex.ying.space.entity.EstateProjectActivityHouseModelItemsExample;
import com.ecoolex.ying.space.entity.EstateProjectActivityHouseModelItemsExample.Criteria;
import com.ecoolex.ying.space.entity.EstateProjectActivityHouseModelItemsGoods;
import com.ecoolex.ying.space.pojo.AddEstateProjectActivityHouseModelItemImagesDTO;
import com.ecoolex.ying.space.pojo.AddEstateProjectActivityHouseModelItemsDTO;

import java.time.Instant;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EstateProjectActivityHouseModelItemsManager {
	
    @Autowired
    private EstateProjectActivityHouseModelItemsDAO dao;
    
    @Autowired
    private EstateProjectActivityHouseModelItemsGoodsManager itemsGoodsManager;
    
    @Autowired
    private EstateProjectActivityGoodsManager goodsManager;
    
    public void edit(Integer modelId,List<AddEstateProjectActivityHouseModelItemsDTO> itemsDTOList, String createUser) {
    	List<EstateProjectActivityHouseModelItems> oldList = listByModelId(modelId);
    	for (int i =0;i<itemsDTOList.size();i++) {
    		AddEstateProjectActivityHouseModelItemsDTO dto = itemsDTOList.get(i);
    		//修改
			if(Check.notNullAndGtZero(dto.getItemId())) {
				EstateProjectActivityHouseModelItems items = oldList.stream().collect(CollectorUtil.first(o -> dto.getItemId().equals(o.getItemId())));
				BizAssert.isTrue(Check.notNull(items),ResultCode.VERIFY.build(30), "户型方案条目Id={}不存在",dto.getItemId());
				BizAssert.isTrue(Check.notNullAndGtZero(dto.getModelId()),ResultCode.VERIFY.build(31), "modelId为空");
				items.setJumpGoodsImage(dto.getJumpGoodsImage());
				items.setName(dto.getName());
				items.setActivityCategoryId(dto.getActivityCategoryId());
				items.setOrdered(dto.getOrdered());
				items.setDescription(dto.getDescription());
				//处理图片
				//取原图片列表
				String oldImages = items.getImages();
				List<String> oldImagesList = JSON.parseArray(oldImages,String.class);
				//传入的图片列表
				List<AddEstateProjectActivityHouseModelItemImagesDTO> imagesList = dto.getActivityHouseModelItemImagesList();
				JSONArray jsonArray = new JSONArray();
				for (AddEstateProjectActivityHouseModelItemImagesDTO imagesDTO : imagesList) {
					String exitImage = oldImagesList.stream().collect(CollectorUtil.first(o -> imagesDTO.getStorageId().equals(o)));
					oldImagesList.remove(exitImage);
					jsonArray.add(imagesDTO.getStorageId());
					//处理图片购物商品
					itemsGoodsManager.edit(items.getItemId(), imagesDTO, createUser);
				}
				//删除移除的图片中的商品
				oldImagesList.forEach(oi -> itemsGoodsManager.deleteByStorageId(oi, createUser));
				items.setImages(jsonArray.toString());
				items.setUpdateUser(createUser);
				items.setUpdateTime(Date.from(Instant.now()));
				dao.updateByPrimaryKey(items);
				oldList.remove(items);
			}else {//新增
				EstateProjectActivityHouseModelItems items = new EstateProjectActivityHouseModelItems();
				items.setJumpGoodsImage(dto.getJumpGoodsImage());
				items.setName(dto.getName());
				items.setActivityCategoryId(dto.getActivityCategoryId());
				items.setOrdered(dto.getOrdered());
				items.setDescription(dto.getDescription());
				items.setModelId(modelId);
				items.setDeleted(EstateProjectActivityHouseModelItems.NOT_DELETED);
				items.setCreateUser(createUser);
				items.setCreateTime(Date.from(Instant.now()));
				items.setUpdateUser(createUser);
				items.setUpdateTime(Date.from(Instant.now()));
				//处理图片
				List<AddEstateProjectActivityHouseModelItemImagesDTO> imagesList = dto.getActivityHouseModelItemImagesList();
				JSONArray jsonArray = new JSONArray();
				for (AddEstateProjectActivityHouseModelItemImagesDTO imagesDTO : imagesList) {
					jsonArray.add(imagesDTO.getStorageId());
				}
				items.setImages(jsonArray.toString());
				dao.insert(items);
				for (AddEstateProjectActivityHouseModelItemImagesDTO imagesDTO : imagesList) {
					//处理图片购物商品
				    itemsGoodsManager.edit(items.getItemId(), imagesDTO, createUser);
				}
			}
		}
    	oldList.forEach(old -> delete(old,createUser));
    }
    
    public void delete(EstateProjectActivityHouseModelItems items,String updateUser) {
    	items.setDeleted(EstateProjectActivityHouseModelItems.IS_DELETED);
    	items.setUpdateUser(updateUser);
    	items.setUpdateTime(Date.from(Instant.now()));
    	dao.updateByPrimaryKey(items);
    }
    
    public List<EstateProjectActivityHouseModelItems> listByModelId(Integer modelId){
    	EstateProjectActivityHouseModelItemsExample example = new EstateProjectActivityHouseModelItemsExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andModelIdEqualTo(modelId).andDeletedEqualTo(EstateProjectActivityHouseModelItems.NOT_DELETED);
    	return dao.selectByExample(example);
    }
    
    public EstateProjectActivityHouseModelItems get(Integer itemId) {
    	return dao.selectByPrimaryKeyWithLogicalDelete(itemId, EstateProjectActivityHouseModelItems.NOT_DELETED);
    }
    
    public List<EstateProjectActivityGoods> listByItemId(Integer itemId) {
    	List<EstateProjectActivityGoods> list = new ArrayList<EstateProjectActivityGoods>();
    	List<EstateProjectActivityHouseModelItemsGoods> modelgoodsList = itemsGoodsManager.listByItemId(itemId);
    	for (EstateProjectActivityHouseModelItemsGoods modelgoods : modelgoodsList) {
			EstateProjectActivityGoods activityGoods = goodsManager.getEstateProjectActivityGoods(modelgoods.getActivityGoodsId());
			list.add(activityGoods);
		}
		return list;
	}
    
    public List<EstateProjectActivityGoods> listByItemIdOnline(Integer itemId) {
    	List<EstateProjectActivityGoods> list = new ArrayList<EstateProjectActivityGoods>();
    	List<EstateProjectActivityHouseModelItemsGoods> modelgoodsList = itemsGoodsManager.listByItemId(itemId);
    	for (EstateProjectActivityHouseModelItemsGoods modelgoods : modelgoodsList) {
			EstateProjectActivityGoods activityGoods = goodsManager.getEstateProjectActivityGoodsOnline(modelgoods.getActivityGoodsId());
			list.add(activityGoods);
		}
		return list;
	}
}